//
//  MyJsonUtils.swift
//  CHSimpleUI
//
//  Created by cyptt on 2023/8/30.
//

import UIKit


public class MyJsonUtils: NSObject {
    
    /*
     json 字符串 转数据
     */
    public static func getArrayFromJSONString(jsonString:String?) ->[Any]?{
        
        if jsonString == nil{
            
            return []
        }
        let jsonData:Data = jsonString!.data(using: .utf8)!
        
        let array = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        
        
        return array as? [Any]
        
    }
    
    /*
     json 字符串转字典
     */
    public static func getDictionaryFromJSONString(jsonString:String?) ->[String:Any]?{
        
        if jsonString == nil{
            
            return [:]
        }
        let jsonData:Data = jsonString!.data(using: .utf8)!
        
        let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        
        return dict as? [String : Any]
        
    }
    
    /*
     json字符串转object
     */
    public static func getObjectFromJSONString(jsonString:String?) ->Any?{
        
        if jsonString == nil{
            
            return [:]
        }
        let jsonData:Data = jsonString!.data(using: .utf8)!
        
        let dict:Any? = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        
        return dict
    }
    
    //数组转json
    public static func getJSONStringFromArray(array:[String:Any]) -> String? {

        if (!JSONSerialization.isValidJSONObject(array)) {

            print("无法解析出JSONString")

            return ""

        }

        let data : NSData? = try? JSONSerialization.data(withJSONObject: array, options: []) as NSData?

        if data == nil{
            return ""
        }
        let JSONString:String? = NSString(data:data! as Data,encoding: String.Encoding.utf8.rawValue) as String?

        return JSONString

    }
    
    /**

         字典转换为JSONString

         - parameter dictionary: 字典参数

         - returns: JSONString

         */

    public static func getJSONStringFromDictionary(dictionary:[String:Any]) -> String?{
        
        if (!JSONSerialization.isValidJSONObject(dictionary)) {
            
            print("无法解析出JSONString")
            
            return ""
            
        }
        
        let data:NSData? = try? JSONSerialization.data(withJSONObject: dictionary, options: []) as NSData?
        if data == nil{
            return ""
        }
        let JSONString = NSString(data:data! as Data,encoding: String.Encoding.utf8.rawValue)
        
        return JSONString! as String
        
    }
    
    
    /*
     object 转 json
     */
    public static func getJSONStringFromObject(obj:Any) -> String?{
        
        let data:NSData? = try? JSONSerialization.data(withJSONObject: obj, options: []) as NSData?
        if data == nil{
            return ""
        }
        let JSONString = NSString(data:data! as Data,encoding: String.Encoding.utf8.rawValue)
        
        return JSONString! as String
        
    }


}
